<?php
namespace Home\Controller;
use Think\Controller;
use Think\Upload;
class NewspushController extends Controller {
	public function newspush_pro(){
	    $data=I("post.");
	    //dump($data);
	    switch ($data['msgtype']){
	        case 'text':
	            $this->chuli("",$data);
	            break;
	        case 'image':
	            $config = array(
	            'maxSize' => 1024000,
	            'rootPath' => './Public/',
	            'savePath' => '/uploads/',
	            'subName' => 'pushnews',
	            'replace' => 'true',
	            'exts' => array('jpg'),
	            );
	            $media_id=$this->up_meiti1($config,$data);
	            $this->chuli($media_id,$data);
	            break;
	        case 'voice':
	            $config = array(
	            'maxSize' => 1024000,
	            'rootPath' => './Public/',
	            'savePath' => '/uploads/',
	            'subName' => 'pushnews',
	            'replace' => 'true',
	            'exts' => array('amr','mp3'),
	            );
	            $media_id=$this->up_meiti1($config,$data);
	            $this->chuli($media_id,$data);
	            break;
	        case 'video':
	            $config = array(
	            'maxSize' => 10240000,
	            'rootPath' => './Public/',
	            'savePath' => '/uploads/',
	            'subName' => 'pushnews',
	            'replace' => 'true',
	            'exts' => array('amr','mp4','jpg'),
	            );
	            $media_id=$this->up_meiti1($config,$data);
	            //dump($media_id);
	            $this->chuli($media_id,$data);
	            break;
	        case 'thumb':
	            $config = array(
	            'maxSize' => 10240000,
	            'rootPath' => './Public/',
	            'savePath' => '/uploads/',
	            'subName' => 'pushnews',
	            'replace' => 'true',
	            'exts' => array('amr','mp3','jpg'),
	            );
	            $media_id=$this->up_meiti1($config,$data);
	            //dump($media_id);
	            $this->chuli($media_id,$data);
	            break;
	        case 'news':
	            $config = array(
	            'maxSize' => 10240000,
	            'rootPath' => './Public/',
	            'savePath' => '/uploads/',
	            'subName' => 'pushnews',
	            'replace' => 'true',
	            'exts' => array('jpg'),
	            );
	            //dump($data);
	            $media_id=$this->up_meiti1($config,$data);
	            //dump($media_id);
	            $this->chuli($media_id,$data);
	            break;
	    }
	}
	public function up_meiti1($config=null,$data=null){
	    $upload = new \Think\Upload($config);
	    $info = $upload -> upload();
	    //dump($info);
	    if (!$info) {
	        $this -> error($upload -> getError());
	    } else {
	        $file = $info['file'];
	        $data['path']=$_SERVER['DOCUMENT_ROOT'] . __ROOT__ . "/Public/uploads/pushnews/".$file['savename'];
	        if($info['filepath']){
	            $data['filepath']=$_SERVER['DOCUMENT_ROOT'] . __ROOT__ . "/Public/uploads/pushnews/".$info['filepath']['savename'];
	        }
	        $type=$data['msgtype'];
	        if($data['msgtype']=="video"){
	            $filepath = $data['path'];
	            $result=upload_file_to_weixin($type,$filepath);
	            $obj=json_decode($result);
	            $media_id[0]=$obj->media_id;
	            $filepath = $data['filepath'];
	            $result=upload_file_to_weixin("thumb",$filepath);
	            $obj=json_decode($result);
	            $media_id[1]=$obj->thumb_media_id;
	            return $media_id;
	        }
	       if($data['msgtype']=="thumb"){
	            $senddata['touser']="";
	            $data['musicurl']=$info['filepath']['savepath'] . $info['filepath']['savename'];
	            $data['hqmusicurl']=$info['filepath']['savepath'] . $info['filepath']['savename'];
	            $filepath = $data['path'];
	            //echo $filepath;
	            $result=upload_file_to_weixin("thumb",$filepath);
	            //echo $result;
	            $obj=json_decode($result);
	            $data['thumb_media_id']=$obj->thumb_media_id;
	            //dump($data);
	            return $data;
	        }
	        if($data['msgtype']=="news"){
	            unset($data["path"]);
	            //dump($data);
	            $articles="";
	            $n=0;
	            foreach ($data as $key => $val){
	                //echo $key."=>".$val."<br/>";
	                if($key=="public_id"||$key=="msgtype"){
	                    continue;
	                }else{
	                    switch ($key%4){
	                        case 0:$articles[$n]['url']=$val;
	                        $arr=C("TMPL_PARSE_STRING");
	                        if($arr['__PUBLIC__']){
	                            $articles[$n]['picurl']=$arr['__PUBLIC__'].$info[$n*4+3]['savepath'].$info[$n*4+3]['savename'];
	                        }else{
	                            $articles[$n]['picurl']=$_SERVER ['HTTP_HOST']."/weiapp/Public".$info[$n*4+3]['savepath'].$info[$n*4+3]['savename'];
	                        }
	                        $n++;
	                        break;
	                        case 1:$articles[$n]['title']=$val;break;
	                        case 2:$articles[$n]['description']=$val;
	                        break;
	                    }
	                }
	            }
	            $data['news']['articles']=$articles;
	            //dump($data);
	            return $data;
	        }
	        $filepath = $data['path'];
	        $result=upload_file_to_weixin($type,$filepath);
	        //echo $result;
	        $obj=json_decode($result);
	        if($obj->created_at){
	            $msgtype=$data['msgtype'];
	            $media_id=$obj->media_id;
	            //echo $media_id;
	            //推送
	            $senddata['touser']="";
	            return $obj->media_id;
	            
	        }else{
	            if(!$obj->errcode){
	                $this -> error("对不起，无响应！");
	            }
	            $this -> error("推送失败！错误代码为".$obj->errcode);
	        }
	    }
	}
	public function chuli($media_id=null,$data){
	    //dump($data);
	    $msgtype=$data['msgtype'];
	    $senddata['touser']="";
	    $senddata['msgtype']=$msgtype;
	    switch ($msgtype){
	        case "text":
	            $senddata[$msgtype]['content']=$data['content'];
	            //echo $senddata[$msgtype]['content'];
	            break;
	            case "news":
	                $senddata["touser"]="";
	                $senddata["msgtype"]=$media_id['msgtype'];
	                $senddata["news"]=$media_id['news'];
	                break;
	            case "image":
	                $senddata[$msgtype]['media_id']=$media_id;
	                break;
	            case "voice":
	                $senddata[$msgtype]['media_id']=$media_id;
	                break;
	            case "video":
	                //dump($media_id);
	                $senddata[$msgtype]['media_id']=$media_id[0];
	                $senddata[$msgtype]['thumb_media_id']=$media_id[1];
	                $senddata[$msgtype]['title']=$data['title'];
	                $senddata[$msgtype]['description']=$data['description'];
	                break;
	            case "thumb":
	                $senddata['msgtype']="music";
	                $senddata["music"]['title']=$media_id['title'];
	                $senddata["music"]['description']=$media_id['description'];
	                $arr=C("TMPL_PARSE_STRING");
	                if($arr['__PUBLIC__']){
	                    $senddata["music"]['musicurl']=$arr['__PUBLIC__'].$media_id['musicurl'];
	                    $senddata["music"]['hqmusicurl']=$arr['__PUBLIC__'].$media_id['hqmusicurl'];
	                }else{
	                    $senddata["music"]['musicurl']=$_SERVER ['HTTP_HOST']."/weiapp/Public/".$media_id['musicurl'];
	                    $senddata["music"]['hqmusicurl']=$_SERVER ['HTTP_HOST']."/weiapp/Public/".$media_id['hqmusicurl'];
	                }
	                $senddata["music"]['thumb_media_id']=$media_id['thumb_media_id'];
	                //dump($senddata);
	                break;
	    }
	    $result=$this->pushnews();
	    //dump($senddata);
	    $data=$result->data;
	    $openid=$data->openid;
	    arrayRecursive($senddata, 'urlencode', true);
	    $truepush=0;
	    $errorpush=0;
	    foreach ($openid as $openid1){
	        $senddata['touser']=$openid1;
	        //dump($senddata);
	        $senddatas=json_encode($senddata);
	        //echo substr(str_replace('\\','',JSON($senddatas)),1,-1);
	        $result=$this->sendnews(substr(str_replace('\\','',JSON($senddatas)),1,-1));
	        //dump($result);
	        $obj=json_decode($result);
	        if($obj->errmsg=="ok"){
	            $truepush++;
	        }else{
	            $errorpush++;
	        }
	    }
	    $obj=json_decode($result);
	    $this -> success("推送成功".$truepush."条信息,失败".$errorpush."条信息.", '../MemberPublic/newspush');
	}
	public function pushnews(){
	    $access_token=get_accesstoken();
	    $url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=".$access_token;
	    $result = https_request($url);
	    //dump($result);
	    $obj=json_decode($result);
	    return $obj;
	}
	public function sendnews($senddata){
	    $access_token=get_accesstoken();
	    $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$access_token;
	    $result = https_request($url,$senddata);
	    return $result;
	}
}

?>